package hui.zhang.service.base.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import hui.zhang.dao.base.CourierRepository;
import hui.zhang.dao.base.FixedAreaRepository;
import hui.zhang.dao.base.TakeTimeRepository;
import hui.zhang.domain.base.Courier;
import hui.zhang.domain.base.FixedArea;
import hui.zhang.domain.base.TakeTime;
import hui.zhang.service.base.FixedAreaService;

@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {
	
	@Autowired
	private FixedAreaRepository fixedAreaRepository;

	@Override
	public void save(FixedArea model) {
		fixedAreaRepository.save(model);
	}

	@Override
	public Page<FixedArea> findAll(Specification<FixedArea> specification, Pageable pageable) {
		return fixedAreaRepository.findAll(specification, pageable);
	}

	//注入快递员dao
	@Autowired
	private CourierRepository courierRepository;
	//注入收派时间
	@Autowired
	private TakeTimeRepository takeTimeRepository;
	
	@Override
	public void associationCourierToFixedArea(FixedArea model, Integer courierId, Integer takeTimeId) {
		FixedArea fixedArea = fixedAreaRepository.findOne(model.getId());
		System.err.println(fixedArea);
		Courier courier = courierRepository.findOne(courierId);
		TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);
		System.err.println(courier);
		System.err.println(takeTime);
		//快递员关联到定区
		fixedArea.getCouriers().add(courier);
		//收派时间关联快递员
		courier.setTakeTime(takeTime);
	}

	@Override
	public List<FixedArea> findAll() {
		return fixedAreaRepository.findAll();
	}

}
